package com.ffox.service.impl;

import com.ffox.entity.SchoolApproval;
import com.ffox.entity.SchoolInformation;
import com.ffox.mapper.PlatformSchoolMapper;
import com.ffox.result.Result;
import com.ffox.service.PlatformSchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlatformSchoolServiceImpl implements PlatformSchoolService {
    @Autowired
    PlatformSchoolMapper platformSchoolMapper;
//       获取所有待审核的学校
    @Override
    public Result<List<SchoolApproval>> getAllSchoolApproval() {
        return Result.success("获取成功",platformSchoolMapper.getAllSchoolApproval());
    }
// 学校审核功能
    @Override
    public Result<Integer> updateSchoolApproval(Long id, Integer status) {
        var authentication = SecurityContextHolder.getContext().getAuthentication();
//        status 0:待审核 1:通过 2:拒绝
        if (status == 1){
            Map <String, Object> params = new HashMap<>();
            params.put("reviewerNum",authentication.getPrincipal());
            params.put("reviewTime",LocalDateTime.now());
            platformSchoolMapper.updateSchoolApproval(id,params);
            Map<String, Object> statu = new HashMap<>();
            statu.put("status", 1);
            platformSchoolMapper.updateSchoolApproval(id,statu);

            SchoolApproval schoolApproval = platformSchoolMapper.getSchoolApprovalById(id);
            SchoolInformation schoolInformation = new SchoolInformation();
            schoolInformation.setNumber(schoolApproval.getNumber());
            schoolInformation.setPassword(schoolApproval.getPassword());
            schoolInformation.setCode(schoolApproval.getCode());
            schoolInformation.setRealName(schoolApproval.getRealName());
            schoolInformation.setMobile(schoolApproval.getMobile());
            schoolInformation.setProvince(schoolApproval.getProvince());
            schoolInformation.setCity(schoolApproval.getCity());
            schoolInformation.setDistrict(schoolApproval.getDistrict());
            schoolInformation.setDetailAddress(schoolApproval.getDetailAddress());
            schoolInformation.setAvatar("http://192.168.190.53:80/group1/M00/00/00/wKi-NWiYwqmARlxuAAEikS43N48254.png");
            schoolInformation.setCreatedAt(LocalDateTime.now());
            Integer result = platformSchoolMapper.insertSchoolInformation(schoolInformation);
            if (result == 1) {
                return Result.success("学校信息添加成功", result);
            } else {
                return Result.fail("学校信息添加失败");
            }
        }
        if (status == 2){
            Map <String, Object> params = new HashMap<>();
            params.put("reviewerNum",authentication.getPrincipal());
            params.put("reviewTime",LocalDateTime.now());
            platformSchoolMapper.updateSchoolApproval(id,params);

            Map<String, Object> statu = new HashMap<>();
            statu.put("status", 2);
            return Result.success("学校申请拒绝成功", platformSchoolMapper.updateSchoolApproval(id,statu));
        }
        return Result.fail("操作失败");
    }

//    获取所有学校信息
    @Override
    public Result<List<SchoolInformation>> getAllSchoolInformation() {
        return Result.success("获取成功",platformSchoolMapper.getAllSchoolInformation());
    }

//    修改学校信息
    @Override
    public Result<Integer> updateSchoolInformation(Long id, Map<String, Object> params) {
        return Result.success("修改成功",platformSchoolMapper.updateSchoolInformation(id,params));
    }

//    修改学校状态
    @Override
    public Result<Integer> updateSchoolStatus(Long id, Integer status) {
//        学校状态 0:正常 1:禁用
        Map<String,Object> params = new HashMap<String,Object>(){{
            put("status",status);
        }};
        return Result.success("修改成功",platformSchoolMapper.updateSchoolInformation(id,params));
    }
}
